package com.yzf;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hzl.Constants;
import com.hzl.ContactsActivity;
import com.hzl.UserInfoBean;
import com.yao.component.circle.R;
import com.yzf.bean.GroupBean;

import java.util.ArrayList;

public class ShowPermissionActivity extends Activity {
    //选中的打勾图标
    private ImageView publicImgTip;
    private ImageView privateImgTip;
    private ImageView someoneImgTip;
    private ImageView nobodyImgTip;
    //当前选中的行文字
    private String permissionString = "";
    //折叠的朋友分组列表
    private ArrayList<GroupBean> bufenGroupBeanList = new ArrayList<GroupBean>();
    private ArrayList<GroupBean> bugeiGroupBeanList = new ArrayList<GroupBean>();
    private GroupBean groupBean = new GroupBean();
    //折叠的朋友分组分表里面的朋友
    private ArrayList<UserInfoBean> bufenUserInfoBeanList = new ArrayList<UserInfoBean>();
    private ArrayList<UserInfoBean> bugeiUserInfoBeanList = new ArrayList<UserInfoBean>();
    private MyListView someOneListView;
    private BufenAdapter bufenAdapter;
    //用来判断当前是选择哪一行，0公开或者私密,1部分可见，2是不给谁看，12再根据里面有没勾选分组信息判断能不能点击完成，否则提示选择一个标签
    private int selectPosition;
    private boolean someoneExpand;//部分可见listview伸缩
    private boolean nobodyExpand;//谁不可见listview伸缩
    private MyListView nobodyListView;
    private BugeiAdapter bugeiAdapter;
    private ImageView someon_view_next;
    private ImageView nobody_view_next;
    private int permissionType;//得到对面是选择了哪种类型跳转过来，1:公开，2:私密，3:部分可见，4:不给谁看

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_permission);
        permissionString = getIntent().getStringExtra("permissionString");
        permissionType = getIntent().getIntExtra("permissionType", 0);
        if (permissionType == 1 || permissionType == 2) {//从上个界面过来1，2是代表选择公开或者私密行
            selectPosition = 0;
        } else if (permissionType == 3) {//从上个界面过来3代表部分可见
            selectPosition = 1;
        } else if (permissionType == 4) {//4代码不给谁看
            selectPosition = 2;
        }
        initView();
    }


    private void initView() {
        someon_view_next = (ImageView) findViewById(R.id.someon_view_next);
        nobody_view_next = (ImageView) findViewById(R.id.nobody_view_next);
        //公开
        findViewById(R.id.public_view).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectPosition = 0;
                someoneExpand = false;
                nobodyExpand = false;
                permissionString = "公开";
                publicImgTip.setVisibility(View.VISIBLE);
                privateImgTip.setVisibility(View.INVISIBLE);
                someoneImgTip.setVisibility(View.INVISIBLE);
                nobodyImgTip.setVisibility(View.INVISIBLE);
                someOneListView.setVisibility(View.GONE);
                nobodyListView.setVisibility(View.GONE);
            }
        });
        //私密
        findViewById(R.id.private_view).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectPosition = 0;
                someoneExpand = false;
                nobodyExpand = false;
                permissionString = "私密";
                publicImgTip.setVisibility(View.INVISIBLE);
                privateImgTip.setVisibility(View.VISIBLE);
                someoneImgTip.setVisibility(View.INVISIBLE);
                nobodyImgTip.setVisibility(View.INVISIBLE);
                someOneListView.setVisibility(View.GONE);
                nobodyListView.setVisibility(View.GONE);
            }
        });
        //部分人可见
        findViewById(R.id.someone_view).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectPosition = 1;
                if (bufenGroupBeanList.size() > 0) {
                    nobodyExpand = false;
                    if (!someoneExpand) {
                        if (!nobodyExpand) {
                            for (int i = 0; i < bufenGroupBeanList.size(); i++) {
                                bufenGroupBeanList.get(i).setChecked(false);
                                bugeiAdapter.notifyDataSetChanged();
                            }
                        }
                        publicImgTip.setVisibility(View.INVISIBLE);
                        privateImgTip.setVisibility(View.INVISIBLE);
                        someoneImgTip.setVisibility(View.VISIBLE);
                        nobodyImgTip.setVisibility(View.INVISIBLE);
                        someOneListView.setVisibility(View.VISIBLE);
                        nobodyListView.setVisibility(View.GONE);
                        someon_view_next.setImageResource(R.drawable.up);
                    } else {
                        publicImgTip.setVisibility(View.INVISIBLE);
                        privateImgTip.setVisibility(View.INVISIBLE);
                        someoneImgTip.setVisibility(View.VISIBLE);
                        nobodyImgTip.setVisibility(View.INVISIBLE);
                        someOneListView.setVisibility(View.GONE);
                        someon_view_next.setImageResource(R.drawable.down);
                        nobodyListView.setVisibility(View.GONE);
                    }
                    someoneExpand = !someoneExpand;
                } else {
                    //如果没有朋友分组就添加
                    Intent intent = new Intent(ShowPermissionActivity.this, ContactsActivity.class);
                    intent.putExtra("type", Constants.NEW_START_LABEL);
                    startActivityForResult(intent, 1);
                }

            }
        });
        //不给谁看
        findViewById(R.id.nobody_view).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectPosition = 2;
                if (bufenGroupBeanList.size() > 0) {
                    someoneExpand = false;
                    if (!nobodyExpand) {
                        if (!someoneExpand) {
                            for (int i = 0; i < bufenGroupBeanList.size(); i++) {
                                bufenGroupBeanList.get(i).setChecked(false);
                                bufenAdapter.notifyDataSetChanged();
                            }
                        }
                        publicImgTip.setVisibility(View.INVISIBLE);
                        privateImgTip.setVisibility(View.INVISIBLE);
                        nobodyImgTip.setVisibility(View.VISIBLE);
                        someoneImgTip.setVisibility(View.INVISIBLE);
                        nobody_view_next.setImageResource(R.drawable.up);
                        nobodyListView.setVisibility(View.VISIBLE);
                        someOneListView.setVisibility(View.GONE);
                    } else {
                        publicImgTip.setVisibility(View.INVISIBLE);
                        privateImgTip.setVisibility(View.INVISIBLE);
                        nobodyImgTip.setVisibility(View.VISIBLE);
                        someoneImgTip.setVisibility(View.INVISIBLE);
                        nobody_view_next.setImageResource(R.drawable.down);
                        someOneListView.setVisibility(View.GONE);
                        nobodyListView.setVisibility(View.GONE);
                    }
                    nobodyExpand = !nobodyExpand;
                } else {
                    //如果没有朋友分组就添加
                    Intent intent = new Intent(ShowPermissionActivity.this, ContactsActivity.class);
                    intent.putExtra("type", Constants.NEW_START_LABEL);
                    startActivityForResult(intent, 1);
                }
            }
        });
        publicImgTip = (ImageView) findViewById(R.id.public_view_tip);
        privateImgTip = (ImageView) findViewById(R.id.private_view_tip);
        someoneImgTip = (ImageView) findViewById(R.id.someone_view_tip);
        nobodyImgTip = (ImageView) findViewById(R.id.nobody_view_tip);
        if ("公开".equals(permissionString)) {
            publicImgTip.setVisibility(View.VISIBLE);
        } else if ("私密".equals(permissionString)) {
            privateImgTip.setVisibility(View.VISIBLE);
        } else if ("部分可见".equals(permissionString)) {
            someoneImgTip.setVisibility(View.VISIBLE);
        } else if ("不给谁看".equals(permissionString)) {
            nobodyImgTip.setVisibility(View.VISIBLE);
        }
        findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShowPermissionActivity.this.finish();
            }
        });
        findViewById(R.id.commit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectPosition == 1) {//部分可见
                    int hasCheckNum = 0;
                    ArrayList<GroupBean> bufenGBList = new ArrayList<GroupBean>();
                    StringBuffer sb = new StringBuffer();
                    for (int i = 0; i < bufenGroupBeanList.size(); i++) {
                        GroupBean groupBean = bufenGroupBeanList.get(i);
                        if (groupBean.isChecked()) {
                            hasCheckNum++;
                            sb.append(groupBean.getName() + ",");
                            bufenGBList.add(groupBean);
                        }
                    }
                    if (sb.length() > 0) {
                        sb.deleteCharAt(sb.length() - 1);
                    }
                    if (hasCheckNum == 0) {
                        Toast.makeText(ShowPermissionActivity.this, "请选择至少一个标签。", Toast.LENGTH_SHORT).show();
                    } else {
                        Intent intent = new Intent();
                        intent.putExtra("bufenGB", sb.toString());
                        intent.putExtra("bufenGBlist", bufenGBList);
                        setResult(Constants.SEND_BUFEN_CODE, intent);
                        ShowPermissionActivity.this.finish();
                    }
                } else if (selectPosition == 2) {//不给谁看
                    int hasCheckNum = 0;
                    ArrayList<GroupBean> bugeiGBList = new ArrayList<GroupBean>();
                    StringBuffer sb = new StringBuffer();
                    for (int i = 0; i < bufenGroupBeanList.size(); i++) {
                        GroupBean groupBean = bufenGroupBeanList.get(i);
                        if (groupBean.isChecked()) {
                            hasCheckNum++;
                            sb.append(groupBean.getName() + ",");
                            bugeiGBList.add(groupBean);
                        }
                    }
                    if (sb.length() > 0) {
                        sb.deleteCharAt(sb.length() - 1);
                    }
                    if (hasCheckNum == 0) {
                        Toast.makeText(ShowPermissionActivity.this, "请选择至少一个标签。", Toast.LENGTH_SHORT).show();
                    } else {
                        Intent intent = new Intent();
                        intent.putExtra("bugeiGB", "除去  " + sb.toString());
                        intent.putExtra("bugeiGBlist", bugeiGBList);
                        setResult(Constants.SEND_BUGEI_CODE, intent);
                        ShowPermissionActivity.this.finish();
                    }
                } else {

                    Intent intent = new Intent();
                    intent.putExtra("shoShowPermission", permissionString);
                    setResult(Constants.SEND_NAME_CODE, intent);
                    ShowPermissionActivity.this.finish();
                }
            }
        });
        //部分可见
        someOneListView = (MyListView) findViewById(R.id.someone_listview);
        bufenAdapter = new BufenAdapter();
        someOneListView.setAdapter(bufenAdapter);
        someOneListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                GroupBean groupBean = bufenGroupBeanList.get(position);
                if (position == bufenGroupBeanList.size() - 1) {
                    Intent intent = new Intent(ShowPermissionActivity.this, LabelActivity.class);
                    intent.putExtra("groupList", bufenGroupBeanList);
                    startActivityForResult(intent, 3);
                    return;
                }
                if (groupBean.isChecked()) {
                    groupBean.setChecked(false);
                } else {
                    groupBean.setChecked(true);
                }
                bufenAdapter.notifyDataSetChanged();
            }
        });
        //谁不可见
        nobodyListView = (MyListView) findViewById(R.id.nobody_listview);
        bugeiAdapter = new BugeiAdapter();
        nobodyListView.setAdapter(bugeiAdapter);
        nobodyListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                GroupBean groupBean = bufenGroupBeanList.get(position);
                if (position == bufenGroupBeanList.size() - 1) {
                    Intent intent = new Intent(ShowPermissionActivity.this, LabelActivity.class);
                    intent.putExtra("groupList", bufenGroupBeanList);
                    startActivity(intent);
                    return;
                }
                if (groupBean.isChecked()) {
                    groupBean.setChecked(false);
                } else {
                    groupBean.setChecked(true);
                }
                bugeiAdapter.notifyDataSetChanged();
            }
        });
        if (permissionType == 1) {//上个界面传值过来是公开
            publicImgTip.setVisibility(View.VISIBLE);
            privateImgTip.setVisibility(View.INVISIBLE);
        }
        if (permissionType == 2) {
            publicImgTip.setVisibility(View.INVISIBLE);
            privateImgTip.setVisibility(View.VISIBLE);

        }
        if (permissionType == 3) {//如果上个界面传过来是3，就显示部分可见按钮
            someoneImgTip.setVisibility(View.VISIBLE);
            someOneListView.setVisibility(View.VISIBLE);
            nobodyListView.setVisibility(View.GONE);
        }
        if (permissionType == 4) {
            nobodyImgTip.setVisibility(View.VISIBLE);
            nobodyListView.setVisibility(View.VISIBLE);
            someOneListView.setVisibility(View.GONE);

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        bufenGroupBeanList.clear();
        bufenGroupBeanList.addAll(Data.bufenGroupBeanList);
        if (bufenGroupBeanList.size() == 0) {
            selectPosition = 0;
            permissionString = "公开";
            publicImgTip.setVisibility(View.VISIBLE);
            if (permissionType == 2) {
                publicImgTip.setVisibility(View.INVISIBLE);
                permissionType = 0;
            }
            someoneImgTip.setVisibility(View.INVISIBLE);
            nobodyImgTip.setVisibility(View.INVISIBLE);
        } else if (bufenGroupBeanList.size() > 0) {
            bufenGroupBeanList.add(new GroupBean());
        }
        if (bufenAdapter != null) {
            bufenAdapter.notifyDataSetChanged();
            bugeiAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            if (requestCode == 1 && resultCode == Constants.CODE_FRIEND_LIST) {
                groupBean.setPersonList((ArrayList<UserInfoBean>) data.getSerializableExtra("UserInfoBean"));
                Intent intent = new Intent(ShowPermissionActivity.this, LabelMemberActivity.class);
                intent.putExtra("groupBean", groupBean);
                intent.putExtra("type", "saveLabel");
                intent.putExtra("selectPosition", selectPosition);
                startActivityForResult(intent, 2);
            } else if (requestCode == 2 && resultCode == Constants.NEW_LABEL_CODE) {
                groupBean = (GroupBean) data.getSerializableExtra("groupBean");
                selectPosition = data.getIntExtra("selectPosition", 0);

                groupBean.getPersonList().remove(groupBean.getPersonList().size() - 2);//移除加好
                groupBean.getPersonList().remove(groupBean.getPersonList().size() - 1);//移除减号
                bufenGroupBeanList.add(groupBean);
//                //在最后一行设置标签行
//                bufenGroupBeanList.add(new GroupBean());
                //暂时没有数据，先把数据保存到Data下
                Data.bufenGroupBeanList.clear();
                Data.bufenGroupBeanList.addAll(bufenGroupBeanList);
                if (bufenGroupBeanList != null) {

                    publicImgTip.setVisibility(View.INVISIBLE);
                    privateImgTip.setVisibility(View.INVISIBLE);
                    if (selectPosition == 1) {//如果是选择部分可见过来的回来时候部分可见listview可见
                        someoneImgTip.setVisibility(View.VISIBLE);
                        someOneListView.setVisibility(View.VISIBLE);
                        nobodyListView.setVisibility(View.GONE);
                    } else if (selectPosition == 2) {//如果是不给谁看过来的时候不给谁看listview可见
                        nobodyImgTip.setVisibility(View.VISIBLE);
                        someOneListView.setVisibility(View.GONE);
                        nobodyListView.setVisibility(View.VISIBLE);
                    }
                    bufenAdapter.notifyDataSetChanged();
                    bugeiAdapter.notifyDataSetChanged();
                }
            } else if (requestCode == 3) {

            }

        }
    }

    class BufenAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return bufenGroupBeanList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View inflate = getLayoutInflater().inflate(R.layout.someone_item, null);
            TextView fenzuTv = (TextView) inflate.findViewById(R.id.fenzu);
            TextView mid = (TextView) inflate.findViewById(R.id.mid);
            GroupBean groupBean = bufenGroupBeanList.get(position);
            String name = groupBean.getName();
            fenzuTv.setText(name);
            CheckBox cb = (CheckBox) inflate.findViewById(R.id.someone_item_cb);
            if (groupBean.isChecked()) {
                cb.setBackgroundResource(R.drawable.tick_select);
            } else {
                cb.setBackgroundResource(R.drawable.tick);
            }
            TextView mingziTv = (TextView) inflate.findViewById(R.id.mingzi);
            ArrayList<UserInfoBean> personList = groupBean.getPersonList();
            StringBuffer sb = new StringBuffer();
            if (personList != null) {
                if (personList.size() > 0) {
                    for (int i = 0; i < personList.size(); i++) {
                        UserInfoBean UserInfoBean = personList.get(i);
                        sb.append(UserInfoBean.getUserName() + ",");
                    }
                    if (sb.length() > 0) {
                        sb.deleteCharAt(sb.length() - 1);
                    }
                }
            }
            mingziTv.setText(sb.toString());
            if (position == bufenGroupBeanList.size() - 1) {
                cb.setVisibility(View.INVISIBLE);
                mid.setText("编辑标签");
                mid.setTextColor(getResources().getColor(R.color.bianjibiaoqian));
                fenzuTv.setVisibility(View.GONE);
                mingziTv.setVisibility(View.GONE);
            }

            return inflate;
        }
    }

    class BugeiAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return bufenGroupBeanList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View inflate = getLayoutInflater().inflate(R.layout.someone_item, null);
            TextView fenzuTv = (TextView) inflate.findViewById(R.id.fenzu);
            TextView mid = (TextView) inflate.findViewById(R.id.mid);
            GroupBean groupBean = bufenGroupBeanList.get(position);
            String name = groupBean.getName();
            fenzuTv.setText(name);
            CheckBox cb = (CheckBox) inflate.findViewById(R.id.someone_item_cb);
            if (groupBean.isChecked()) {
                cb.setBackgroundResource(R.drawable.tick_red_select);
            } else {
                cb.setBackgroundResource(R.drawable.tick);
            }
            TextView mingziTv = (TextView) inflate.findViewById(R.id.mingzi);
            ArrayList<UserInfoBean> personList = groupBean.getPersonList();
            StringBuffer sb = new StringBuffer();
            if (personList != null) {
                if (personList.size() > 0) {
                    for (int i = 0; i < personList.size(); i++) {
                        UserInfoBean UserInfoBean = personList.get(i);
                        sb.append(UserInfoBean.getUserName() + ",");
                    }
                    if (sb.length() > 0) {
                        sb.deleteCharAt(sb.length() - 1);
                    }
                }
            }
            mingziTv.setText(sb.toString());
            if (position == bufenGroupBeanList.size() - 1) {
                cb.setVisibility(View.INVISIBLE);
                mid.setText("编辑标签");
                mid.setTextColor(getResources().getColor(R.color.bianjibiaoqian));
                mingziTv.setVisibility(View.GONE);
                fenzuTv.setVisibility(View.GONE);
            }

            return inflate;
        }
    }
}
